package com.aixin.project.util;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Sufeng
 * Date: 12-5-13
 * Time: 下午11:02
 * To change this template use File | Settings | File Templates.
 */
public class Pagenation2 {
    private int maxPage = 10;
    private int pagenum;

    private int currentpage;

    private List data;

    private int totalnum;
    
    private String basePath;
    
    private String params;

    public Pagenation2(int currentpage, int pagenum, int totalnum, List data,String basePath, String params) {
        this.currentpage = currentpage;
        this.pagenum = pagenum;
        this.totalnum = totalnum;
        this.data = data;
        this.basePath = basePath;
        this.params = params;
    }



    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
    }

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    public int getTotalPage(){
        return (totalnum+pagenum-1)/pagenum;
    }

    public boolean hasNext(){
        if(getTotalPage()>currentpage)
            return true;
        return false;
    }

    public boolean hasPre(){
        if(currentpage>1)
            return true;
        return false;
    }

    public int getPagenum() {
        return pagenum;
    }

    public void setPagenum(int pagenum) {
        this.pagenum = pagenum;
    }

    public int getCurrentpage() {
        return currentpage;
    }

    public void setCurrentpage(int currentpage) {
        this.currentpage = currentpage;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public int getTotalnum() {
        return totalnum;
    }

    public void setTotalnum(int totalnum) {
        this.totalnum = totalnum;
    }
    
    public String print(){
        int startPage = 1;
        int endPage = getTotalPage();
        if(getTotalPage()>maxPage){
            if(currentpage-maxPage/2<=0) {
                startPage = 1;
                endPage = maxPage;
            }else if(currentpage+maxPage/2>=getTotalPage()){
                endPage = getTotalPage();
                startPage = getTotalPage()-maxPage+1;
            }else{
                startPage = currentpage-maxPage/2+1;
                endPage = currentpage+maxPage/2;
            }
        }
/*        int startPage = (currentpage-maxPage/2)>0?(currentpage-maxPage/2):1;
        int endPage = (currentpage+maxPage/2)>getTotalPage():getTotalPage():*/
        StringBuffer sb = new StringBuffer();
        sb.append("<div class=\"pagen\"> ");
        if(currentpage>1){
            sb.append("<span class=\"prev_btn\"><a href=\""+basePath+"?p="+(currentpage-1)+/*(params==null?"":("&"+params))+*/"\">上一页</a></span>");
        }
        for(int i=startPage;i<=endPage;i++){
            if(i==currentpage)
                sb.append("<span class=\"now\"><a href=\"#\">"+currentpage+"</a></span>");
            else
                sb.append("<span><a href=\""+basePath+"?p="+i+/*(params==null?"":("&"+params))+*/"\">"+i+"</a></span>");
        }
        if(currentpage<getTotalPage()){
            sb.append("<span class=\"next_btn\"><a href=\""+basePath+"?p="+(currentpage+1)+/*(params==null?"":("&"+params))+*/"\">下一页</a></span>  ");
        }
        sb.append("</div>") ;
        return sb.toString();
    }
}
